package com.whmall.service.crm.provider;

import com.whmall.component.entity.dto.*;
import com.whmall.component.provider.AbstractSameProvider;
import com.whmall.service.crm.api.CompanyApi;
import com.whmall.service.crm.entity.dto.company.*;
import com.whmall.service.crm.entity.dto.company.graph.CompanyGraphDataReqDTO;
import com.whmall.service.crm.entity.dto.company.graph.CompanyGraphDataRspDTO;
import com.whmall.service.crm.entity.dto.company.graph.node.CompanyGraphNode;
import com.whmall.service.crm.module.company.service.CompanyLogService;
import com.whmall.service.crm.module.company.service.CompanyService;
import com.whmall.service.crm.module.companyuser.CompanyUserRepository;
import com.whmall.service.crm.module.companyuser.entity.CompanyUserDO;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@DubboService(interfaceClass = CompanyApi.class,
        methods = {@Method(name = "employeeDepartmentPathChanged", timeout = 30000),
                @Method(name = "query4TestingOrderJobHandler", timeout = 30000),
                @Method(name = "update4TestingOrderJobHandler", timeout = 30000),
                @Method(name = "listAll", timeout = 60000)})
@RequiredArgsConstructor
public class CompanyProvider extends AbstractSameProvider<CompanyService, CompanyQueryCondition> implements CompanyApi {

    private final CompanyUserRepository companyUserRepository;
    private final CompanyLogService companyLogService;


    @Override
    public CompanyGraphDataRspDTO findOrderDetailGraphDataById(Long id, CompanyGraphDataReqDTO request) {
        List<CompanyGraphDataRspDTO> list = findOrderDetailGraphDataByIds(Collections.singletonList(id), request);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<CompanyGraphDataRspDTO> findOrderDetailGraphDataByIds(List<Long> ids, CompanyGraphDataReqDTO request) {
        return service.findOrderDetailGraphDataByIds(ids, request);
    }

    @Override
    public List<CompanyGraphNode> list(CompanyListByReqDTO reqDTO) {
        return service.list(reqDTO);
    }

    @Override
    public Long createCompany(CompanyAddReqDTO companyAddReqDTO) {
        return service.createCompany(companyAddReqDTO);
    }

    @Override
    public Long syncCreateCompany(CompanySyncAddReqDTO addReqDTO, OperatorReqDTO operator) {
        return service.syncCreateCompany(addReqDTO, operator);
    }

    @Override
    public Map<String, Long> batchSyncCreateCompany(List<CompanySyncAddReqDTO> list, OperatorReqDTO operator) {
        return service.batchSyncCreateCompany(list, operator);
    }

    @Override
    public void updateCompany(Long companyId, CompanyAddReqDTO companyAddReqDTO) {
        service.updateCompany(companyId, companyAddReqDTO);
    }

    @Override
    public void updateCompanyBasic(Long companyId, CompanyAddReqDTO companyAddReqDTO) {
        service.updateCompanyBasic(companyId, companyAddReqDTO);
    }

    @Override
    public void updateCompanyEnterpriseType(CompanyUpdateEnterpriseTypeReqDTO companyUpdateEnterpriseTypeReqDTO, OperatorReqDTO operatorReqDTO) {
        service.updateCompanyEnterpriseType(companyUpdateEnterpriseTypeReqDTO, operatorReqDTO);
    }

    @Override
    public CompanyQueryRspDTO queryCompanyById(Long companyId) {

        return service.queryCompanyById(companyId);
    }

    @Override
    public CompanyQueryRspDTO queryCustomerInfo(Long companyId) {
        return service.queryCustomerInfo(companyId);
    }

    @Override
    public List<CompanyRspDTO> queryCompanyByIds(List<Long> ids) {
        List<CompanyRspDTO> companyRspDTOS = service.queryCompanyByIds(ids);
        List<Long> companyIds = new ArrayList<>();
        List<Long> companyDefaultUserIds = new ArrayList<>();
        if (companyRspDTOS != null && companyRspDTOS.size() > 0) {
            companyRspDTOS.forEach(e -> {
                companyIds.add(e.getId());
            });
            List<CompanyUserDO> companyUserDOS = companyUserRepository
                    .listForDefaultUsersByCompanyIdIn(companyIds);
            companyUserDOS.forEach(companyUserDO -> {
                companyDefaultUserIds.add(companyUserDO.getCompanyId());
            });
            companyRspDTOS.forEach(company -> {
                if (companyDefaultUserIds.contains(company.getId())) {
                    company.setIsDefaultAccount(1);
                } else {
                    company.setIsDefaultAccount(0);
                }
            });
        }
        return companyRspDTOS;
    }

    @Override
    public Map<Long, CompanyKeyFieldDTO> listByIds(List<Long> ids) {
        return service.listByIds(ids);
    }

    @Override
    public List<CompanyKeyFieldDTO> listBy(CompanyListByReqDTO reqDTO) {
        return service.listBy(reqDTO);
    }

    @Override
    public List<Long> listByTraderId(Long id) {
        return service.listByTraderId(id);
    }

    @Override
    public Map<Long, CompanyKeyFieldDTO> listAll() {
        return service.listAll();
    }

    @Override
    public void logout(CompanyLogoutReqDTO reqDTO) {
        service.logout(reqDTO);
    }

    @Override
    public void cancelLogout(Long id, Long employeeId) {
        service.cancelLogout(id, employeeId);
    }

    @Override
    public void updateSupplier(Long companyId, CompanyAddReqDTO companyAddReqDTO) {
        service.updateSupplier(companyId, companyAddReqDTO);
    }

    @Override
    public void updateCustomer(Long id, CompanyUpdateCustomerReqDTO reqDTO) {
        service.updateCustomer(id, reqDTO);
    }

    @Override
    public void updateBatch(CompanyUpdateBatchReqDTO reqDTO) {
        service.updateBatch(reqDTO);
    }

    @Override
    public List<CompanyManagerUpdateBatchRspDTO> updateManagerBatch(CompanyManagerUpdateBatchReqDTO reqDTO) {
        return service.updateManagerBatch(reqDTO);
    }

    @Override
    public void updateManager(CompanyManagerUpdateReqDTO reqDTO) {
        service.updateManager(reqDTO);
    }

    @Override
    public void updateBdBySystem(CompanyManagerUpdateReqDTO reqDTO) {
        service.updateBdBySystem(reqDTO);
    }

    @Override
    public Integer nameExists(Long id, String name) {
        return service.nameExists(id, name);
    }

    @Override
    public List<CompanyListByNameRspDTO> listByName(String name, Integer companyAttribute, Integer limitNum) {
        return service.listByName(name, companyAttribute, limitNum);
    }

    @Override
    public CompanyListByNameRspDTO getByName(String name) {
        return service.getByName(name);
    }

    @Override
    public Long getOneManager(Integer targetPositionId, Boolean isNewCustomer, Map<Object, Object> params, List<Long> candidates) {
        return service.getOneManager(targetPositionId, isNewCustomer, params, candidates);
    }

    @Override
    public void updateTime(CompanyUpdateTimeReqDTO reqDTO) {
        service.updateTime(reqDTO);
    }

    @Override
    public void employeeDepartmentPathChanged(EmployeeDepartmentPathChangedReqDTO reqDTO) {
        service.employeeDepartmentPathChanged(reqDTO);
    }

    @Override
    public Map<Integer, List<Long>> query4TestingOrderJobHandler() {
        return service.query4TestingOrderJobHandler();
    }

    @Override
    public void update4TestingOrderJobHandler(CompanyUpdate4TestingOrderJobHandlerReqDTO reqDTO) {
        service.update4TestingOrderJobHandler(reqDTO);
    }

    @Override
    public void fillData(CompanyFillDataReqDTO reqDTO) {
        service.fillData(reqDTO);
    }

    @Override
    public CompanyUpdateApplyDTO updateApply(CompanyUpdateApplyDTO reqDTO) {
        return service.updateApply(reqDTO);
    }

    @Override
    public void updatePrecursorEnterpriseId(Long id, Long precursorEnterpriseId, OperatorReqDTO operatorReqDTO) {
        service.updatePrecursorEnterpriseId(id, precursorEnterpriseId, operatorReqDTO);
    }

    @Override
    public CompanyInformationRspDTO informationById(Long id) {
        return service.informationById(id);
    }

    @Override
    public List<CompanyInformationRspDTO> informationByIds(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return new ArrayList<>(0);
        }
        return service.informationByIds(ids);
    }

    @Override
    public List<CompanyInformationRspDTO> findAssociatedCompany(Long id) {
        return service.findAssociatedCompany(id);
    }

    @Override
    public List<CompanyInformationRspDTO> informationByIds(List<Long> ids, Long queryAssociatedCompanyID) {
        if (ids == null || ids.isEmpty()) {
            return new ArrayList<>(0);
        }
        return service.informationByIds(ids).stream().filter(i -> i.getAssociatedCompanyIdList().contains("-" + queryAssociatedCompanyID + "-")).collect(Collectors.toList());
    }

    @Override
    public void updateCompanyInformation(CompanyInformationDTO companyInformation) {
        service.updateCompanyInformation(companyInformation);
    }

    @Override
    public PageRspDTO<CompanyLogRspDTO> listCompanyLog(PageReqDTO pageReqDTO, CompanyLogListReqDTO reqDTO) {
        return companyLogService.listCompanyLog(pageReqDTO, reqDTO);
    }

    @Override
    public void createCompanyLog(CompanyLogCreateReqDTO createReqDTO) {
        companyLogService.createCompanyLog(createReqDTO);
    }

    @Override
    public void updateCustomerSettlementMethod(Long id, Integer customerSettlementMethod, OperatorReqDTO operatorReqDTO) {
        service.updateCustomerSettlementMethod(id, customerSettlementMethod, operatorReqDTO);
    }

    @Override
    public void updateSupplierSettlementMethod(Long id, Integer supplierSettlementMethod, OperatorReqDTO operatorReqDTO) {
        service.updateSupplierSettlementMethod(id, supplierSettlementMethod, operatorReqDTO);
    }

    @Override
    public List<CompanyKeyFieldDTO> listByPrecursorEnterpriseId(Long precursorEnterpriseId) {
        return service.listByPrecursorEnterpriseId(precursorEnterpriseId);
    }

    @Override
    public PageRspDTO<CompanyKeyFieldDTO> lostCustomerList(PageReqDTO pageReqDTO, LostCompanyListQueryReqDTO reqDTO) {
        return service.lostCustomerList(pageReqDTO, reqDTO);
    }

    @Override
    public void relevanceCompany(CompanyRelevanceCompanyReqDTO reqDTO) {
        service.relevanceCompany(reqDTO);
    }

    @Override
    public void setNoDevelopmentValue(CompanyNoDevelopmentValueReqDTO reqDTO) {
        service.setNoDevelopmentValue(reqDTO);
    }

    @Override
    public List<Long> listByTraderDepartment(String departmentPath) {
        return service.listByTraderDepartment(departmentPath);
    }

    @Override
    public List<Long> listByTraderDepartmentPath(String departmentPath) {
        return service.listByTraderDepartmentPath(departmentPath);
    }

    @Override
    public List<CompanyNoTypeRspDTO> listNoCompanyTypeCustomerList() {
        return service.listNoCompanyTypeCustomerList();
    }

    @Override
    public Long companyBasicInfoApply(CompanyBasicInfoApplyDTO basicInfoApplyDTO, OperatorReqDTO operatorReqDTO) {
        return service.companyBasicInfoApply(basicInfoApplyDTO, operatorReqDTO);
    }

    @Override
    public void companyBasicInfoAudit(ProcessInstanceCompletedMessageReqDTO finishMessage) {
        service.companyBasicInfoAudit(finishMessage);
    }

    @Override
    public void updateLock(Long companyId, OperatorReqDTO operator) {
        service.updateLock(companyId, operator);
    }

    @Override
    public List<List<Long>> getCompanyAssociatedList(List<Long> companyIdList) {
        return service.getCompanyAssociatedList(companyIdList);
    }

    @Override
    public Map<Long, Long> getAllParentCompany() {
        return service.getAllParentCompany();
    }

    public List<Long> listByPurchaserInfo(Long purchaserId, String purchaserDepartmentPath) {
        return service.listByPurchaserInfo(purchaserId, purchaserDepartmentPath);
    }

    @Override
    public PageRspDTO<CompanyKeyFieldDTO> pageWithRegister(PageReqDTO pageReqDTO, CountWithRegisterReqDTO condition) {
        return service.pageWithRegister(pageReqDTO, condition);
    }

    @Override
    public Long countWithRegister(CountWithRegisterReqDTO condition) {
        return service.countWithRegister(condition);
    }

    @Override
    public void updateCvfvairById(Long companyId, Integer hasCvfvair, OperatorReqDTO operator) {
        service.updateCvfvairById(companyId, hasCvfvair, operator);
    }

    @Override
    public List<Long> selectByIdsShareLock(List<Long> ids) {
        return service.selectByIdsShareLock(ids);
    }

    @Override
    public void setBillingBlackList(Long id, Integer inBillingBlacklist, String comment, OperatorReqDTO operatorReqDTO) {
        service.setBillingBlackList(id, inBillingBlacklist, comment, operatorReqDTO);
    }

    @Override
    public void updateCcp(Long id, UpdateCcpReqDto reqDto, OperatorReqDTO operatorReqDTO) {
        service.updateCcp(id, reqDto, operatorReqDTO);
    }

    @Override
    public OrderTradingEntityRspDto getOrderTradingEntities(OrderTradingEntityReqDto reqDto) {
        return service.getOrderTradingEntities(reqDto);
    }
}
